/*
Copyright 2010  Murali Devi (profdevi@gmail.com)

This file is part of ComineGL Project.

ComineGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ComineGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ComineGL.  If not, see <http://www.gnu.org/licenses/>.

This code uses the irrlicht and irrklang libraries.  Please read
the libraries copyrights at 

	irrlicht library:  http://irrlicht.sourceforge.net
	irrklang library:  http://www.ambiera.com/irrklang

*/



//v0.14 copyright Comine.com 20101212U01
#include <windows.h>
#include <stdarg.h>
#include "MStdLib.h"
#include "Global.h"
#include "ComineGLFont.h"


//***********************************************
//** Exported Functions
//***********************************************
MDLLFUN bool CGLFontLoadFile(int fontid,const char *filename,int rows,int cols,const char *datastr)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(rows<=0 || cols<=0 || datastr==NULL || *datastr==0)
		{
		MStdPrintf("**Bad parameters for font load\n");
		return false;
		}

	// Allocate the resource
	IImage *originalimage=GlobalRefVideoDriver->createImageFromFile(filename);
	if(originalimage==NULL)
		{
		MStdPrintf("**Unable to load font file %s\n",filename);
		return false;
		}

	// Check if fontid exists already
	if(GlobalFontList.IsKey(fontid)==true)
		{
		GlobalFontList.Remove(fontid);
		MStdPrintf("Removing Existing Font id %d\n",fontid);
		return false;
		}

	TRefCount<MCGLFontInfo> *newfont=new TRefCount<MCGLFontInfo>;
	if(newfont==NULL)
		{
		originalimage->drop();
		MStdPrintf("**Unable to alloc memory\n");
		return false;
		}

	if(newfont->Create(GlobalRefVideoDriver,originalimage,rows,cols,datastr)==false)
		{
		MStdPrintf("**Unable to init font info\n");
		newfont->Drop();
		return false;
		}
	
	// Add to the table
	if(GlobalFontList.Add(fontid,newfont)==false)
		{
		MStdPrintf("**Unable to add font id %d to font table\n",fontid);
		newfont->Drop();
		return false;
		}

	// Drop Reference to new font
	newfont->Drop();

	MStdPrintf("Added Font File %s as id %d\n",filename,fontid);
	return true;
	}


//////////////////////////////////////////////
MDLLFUN bool CGLFontExists(int fontid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}
	
	return GlobalFontList.IsKey(fontid);
	}


/////////////////////////////////////////////////////////////
MDLLFUN bool CGLFontUnload(int fontid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(GlobalFontList.Remove(fontid)==false)
		{
		MStdPrintf("**Unable to unload font %d\n",fontid);
		return false;
		}

	return true;	
	}


/////////////////////////////////////////////////////////////
MDLLFUN bool CGLFontUnloadAll(void)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(GlobalFontList.Clear()==false)
		{
		MStdPrintf("**Unable to clear all the fonts",__LINE__);
		return false;
		}	
	
	return true;
	}


///////////////////////////////////////////////////
MDLLFUN int CGLFontGetCount(void)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return 0;
		}

	return GlobalFontList.GetCount();	
	}


